package ph.com.gs3.formalistics.model.valueobjects.business;

import java.io.Serializable;

import org.json.JSONException;
import org.json.JSONObject;

public class User implements Serializable {

	private static final long serialVersionUID = -3798985922783499024L;

	public static final class WEB_KEY {

		// @formatter:off

		// Web Keys
		public static final String WEB_ID = "id"; // this should be separate
													// from the actual id since
													// this may duplicate
		public static final String EMAIL = "email";
		public static final String IMAGE_URL = "image_URL";
		public static final String DISPLAY_NAME = "display_name";
		public static final String USER_LEVEL = "user_level_id";

		// Application Keys
		// public static final String ID = "_id";
		public static final String PASSWORD = "password";
		public static final String SERVER = "server";
		public static final String IS_ACTIVE = "is_active";

		// @formatter:on

	}

	public static final class USER_LEVEL {
		public static final int USER = 3;
		public static final int ADMIN = 2;
	}

	public static final class ACTIVITY {
		public static final int ACTIVE = 1;
		public static final int INACTIVE = 0;
	}

	private int dbId;

	private int webId;
	private String email;
	private String displayName;
	private String imageURL;
	private String password;
	private int userLevel;

	private String accountServer;
	private int activity;

	public static User createFromJSON(JSONObject rawUserJSON) throws UserParseException {

		User user = new User();

		String currentField = WEB_KEY.WEB_ID;

		try {
			user.setWebId(rawUserJSON.getInt(WEB_KEY.WEB_ID));
			currentField = WEB_KEY.EMAIL;
			user.setEmail(rawUserJSON.getString(WEB_KEY.EMAIL));
			currentField = WEB_KEY.DISPLAY_NAME;
			user.setDisplayName(rawUserJSON.getString(WEB_KEY.DISPLAY_NAME));
			currentField = WEB_KEY.IMAGE_URL;
			user.setImageURL(rawUserJSON.getString(WEB_KEY.IMAGE_URL));
			currentField = WEB_KEY.USER_LEVEL;
			user.setUserLevel(rawUserJSON.getInt(WEB_KEY.USER_LEVEL));
		} catch (JSONException e) {
			throw new UserParseException(
			        "Server gave an invalid response. Unable to create user from JSON object, "
			                + currentField + " seems to be missing or has invalid format.");
		}

		return user;

	}

	/**
	 * Creates a User object from the given content values.
	 * 
	 * @param cv
	 *            the content value to base the user on.
	 * @return a new User object based on the passed ContentValues object
	 */
	// public static User createFromContentValues(ContentValues cv) {
	//
	// User user = new User();
	//
	// user.setWebId(cv.getAsInteger(WEB_KEY.ID));
	// user.setEmail(cv.getAsString(WEB_KEY.EMAIL));
	// user.setDisplayName(cv.getAsString(WEB_KEY.DISPLAY_NAME));
	// user.setImageURL(cv.getAsString(WEB_KEY.IMAGE_URL));
	//
	// // TODO: add a decryption task here
	// user.setPassword(cv.getAsString(WEB_KEY.PASSWORD));
	// user.setAccountServer(cv.getAsString(WEB_KEY.SERVER));
	// user.setUserLevel(cv.getAsInteger(WEB_KEY.USER_LEVEL) == null ?
	// USER_LEVEL.USER :
	// cv
	// .getAsInteger(WEB_KEY.USER_LEVEL));
	//
	// return user;
	//
	// }

	@Override
	public String toString() {
		return this.dbId + " " + this.displayName;
	}

	// ========================================================================
	// Exclusive Exceptions

	public static class UserParseException extends Exception {

		private static final long serialVersionUID = 6015202562655877661L;

		public UserParseException(String message) {
			super(message);
		}

	}

	// ========================================================================
	// Getters and Setters

	public int getWebId() {
		return webId;
	}

	public void setWebId(int webId) {
		this.webId = webId;
	}

	public int getId() {
		return dbId;
	}

	public void setId(int id) {
		this.dbId = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getImageURL() {
		return imageURL;
	}

	public void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getUserLevel() {
		return userLevel;
	}

	public void setUserLevel(int userLevel) {
		this.userLevel = userLevel;
	}

	public String getAccountServer() {
		return accountServer;
	}

	public void setAccountServer(String accountServer) {
		this.accountServer = accountServer;
	}

	public int getActivity() {
		return activity;
	}

	public void setActivity(int activity) {
		this.activity = activity;
	}

}
